package com.cloudmusic.service.impl;

import com.cloudmusic.dao.UserMapper;
import com.cloudmusic.entity.User;
import com.cloudmusic.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {

	@Autowired
	UserMapper usermapper;


	public List<Map<String, Object>> getUserList() {
		return usermapper.getUserList();
	}


	public List<User> getUsersList(String username) {
		return usermapper.getUsersList(username);
	}

	public User getUserById(Integer id) {
		return usermapper.getUserById(id);
	}

	//用户注册
	public void userSignIn(User user,String uid,String likelid,String collectionlid,String historylid) {
		//添加平台用户信息
		usermapper.addUser(uid, user.getUsername(), user.getPassword());
		//添加用户初始表信息（添加表与中间表）
		usermapper.inserLikeToSongList(likelid);
		usermapper.inserCollectionToSongList(collectionlid);
		usermapper.inserLikeToUserSongList(uid, likelid);
		usermapper.inserCollectionToUserSongList(uid, collectionlid);
		usermapper.insertHistoryList(uid,historylid);
		usermapper.insertHistoryToUserSongList(uid,historylid);
		//初始化用户个人信息
		usermapper.insertUserInfo(uid,user.getUsername());

	}

	//查找用户历史歌单Id
	@Override
	public String selectUserHistoryListId(String uid) {
		String historylid = usermapper.selectUserHistoryListId(uid);
		return historylid;
	}

	//进行音乐最后播放时间的更新
	@Override
	public void playMusic(String historylid, String sid) {
		int i = usermapper.checkMusic(historylid,sid);
	    if(i == 0){
			usermapper.addHistoryPlayMusic(historylid,sid);
		}else{
			usermapper.updateHistoryPlayMusic(historylid, sid);
		}
	    usermapper.updateMusicPlayTimes(sid);
	}

	//更新用户个人信息
	@Override
	public void updateUserInfo(String nname, String sex, String phone, String email, String uid) {
		usermapper.updateUserInfo(nname,sex,phone,email,uid);
	}

	@Override
	public List<Map<String, Object>> selectUserInfo(String uid) {
		List<Map<String, Object>> list = usermapper.selectUserInfo(uid);
		return list;
	}

	//注销用户
	@Override
	public int banUser(String uid) {
		int result = 0;
		try{
			//注销用户，逻辑删除（冻结账户），也不进行物理删除其所拥有的歌单，方便其账户找回
//			usermapper.updateUserState(uid);
			//物理删除账号，包括歌单个人信息、账号信息
			usermapper.deleteUserSongList(uid);
			usermapper.deleteUser(uid);

			result = 1;
		}catch (Exception e){
			result = 0;
		}
		return result;
	}

	//修改用户密码
	@Override
	public int changeUserPassword(String uid, String oldpassword, String newpassword) {
		int result = 0;
		try{
			String originalpassword = usermapper.selectUserPassword(uid);
			if (!oldpassword.equals(originalpassword)){
				result = 2;
			}else {
				usermapper.updateUserPassword(uid,newpassword);
				result = 1;
			}
		}catch (Exception e){
			result = 0;
		}
		return result;
	}


	//获取用户所具有的自定义歌单
	public List<Map<String,Object>> findSongLists(Object userId) {
		return usermapper.findSongLists(userId);
	}

	public String isExitUserName(String signInUserName) {
		return usermapper.isExitUserName(signInUserName);
	}




	
}
